package com.sky.controller.user;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 菜品管理
 */
@RestController("userDishController")
@RequestMapping("/user/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    //--7--根据分类id查询菜品  http://localhost/api/dish/list?categoryId=11
    @GetMapping("list")
    @ApiOperation("根据分类id查询菜品 ")
    public Result selectDishByCategoryId(Long categoryId){
        log.info("根据分类id查询菜品，参数为：{}", categoryId);

        //-1-先查缓存  dish_18
        List<DishVO> dishVOList=(List<DishVO>)redisTemplate.opsForValue().get("dish_"+categoryId);
        if(dishVOList!=null && dishVOList.size()>0){
            return Result.success(dishVOList);
        }

        //-2-缓存没有  dish_18
        List<DishVO> dishVOS=dishService.selectDishByCategoryId(categoryId);
        redisTemplate.opsForValue().set("dish_"+categoryId,dishVOS);
        return Result.success(dishVOS);
    }
}

